package com.originskyseed.platform.login.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.login.i18n.LoginResponseMsg;
import com.originskyseed.platform.login.vo.response.LoadNavMenuResponseVO;
import com.originskyseed.platform.role.model.Menu;
import com.originskyseed.platform.role.model.Role;
import com.originskyseed.platform.role.service.IMenuService;
import com.originskyseed.platform.role.service.IRoleService;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IUserInfoService;

/**
 * 首页Controller类
 * 
 * @author LiangMingHui
 *
 */
@Controller
@RequestMapping("")
@BaseLocaleResource(localeFileName = "i18n_login")
public class IndexController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

	@DubboReference
	private IMenuService menuService;

	@DubboReference
	private IUserInfoService userInfoService;

	@DubboReference
	private IRoleService roleService;

	/**
	 * 加载一级导航菜单
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/loadFirstNavMenu.json")
	public BaseResponseBody loadFirstNavMenu(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 获取当前用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			if (null != userInfo) {
				// 一级菜单
				List<Menu> firstLevelMenus = new ArrayList<>();
				List<Role> roleList = this.roleService.selectByUserId(userInfo.getId());
				for (Role role : roleList) {
					// 获取一级菜单
					List<Menu> menuList = this.menuService.selectFirstMenuByRoleCode(role.getCode());

					firstLevelMenus.addAll(menuList);

				}
				// 加载导航菜单响应VO
				LoadNavMenuResponseVO responseVO = new LoadNavMenuResponseVO();

				responseVO.setFirstLevelMenus(firstLevelMenus);
				result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), LoginResponseMsg.SUCCESS.getMsg()), responseVO);
			} else {
				result = new BaseResponseBody(true, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), LoginResponseMsg.ERROR.getMsg()), null);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), LoginResponseMsg.ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 加载二级导航菜单
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/loadSecondNavMenu.json")
	public BaseResponseBody loadSecondNavMenu(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class); // 获取当前用户信息
			if (null != userInfo&&userInfo.getSourceSys()!=null) {
				// 获取二级和三级菜单
				String parentId = this.menuService.selectParentIdBySysType(userInfo.getSourceSys());

				Map<String, Object> map = new HashMap<>();
				map.put("parentId", parentId);
				List<Menu> secondLevelMenus = this.menuService.selectSecondLevelMenus(userInfo.getId(), map);
				List<Menu> thirdLevelMenus = this.menuService.selectThirdLevelMenus(userInfo.getId(), map);

				// 加载导航菜单响应VO
				LoadNavMenuResponseVO responseVO = new LoadNavMenuResponseVO();
				responseVO.setSecondLevelMenus(secondLevelMenus);
				responseVO.setThirdLevelMenus(thirdLevelMenus);
				result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), LoginResponseMsg.SUCCESS.getMsg()), responseVO);
			} else {
				result = new BaseResponseBody(true, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), LoginResponseMsg.SUCCESS.getMsg()), null);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), LoginResponseMsg.ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
